package drugdealer.data;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Helper class to manage DB related functions for the "Usuario" class.
 * 
 * @author dave
 *
 */
public class UsuarioDAO {
	
	
	
	/**
	 * Static method to retrieve a given user's type from the database.
	 * 
	 * @param email uniquely identifies the user.
	 * @return a String containing the user type "admin", "medico" or "paciente".
	 */
	public static String getTipo(String email) {
		// Get ConnectionPool
		ConnectionPool pool = ConnectionPool.getInstance();

		// Open DB connection
		Connection connection = pool.getConnection();

		try {
			ResultSet rs = connection.createStatement().executeQuery(
					"SELECT Tipo FROM UsuarioTipo WHERE Email = '" + email
							+ "'");
			rs.next();
			return rs.getString(1);

		} catch (SQLException sqle) {
			// todo exceptions throwing
			System.out.println("SHIT!");
			sqle.printStackTrace();
			return null;
		} finally {
			// Close DB connection
			pool.freeConnection(connection);
		}
	}
}
